var t = getApp()
Page({
  data: {
    phone_type:'',
    carnum: [],
    showNewPower: false,
    KeyboardState: false,
    navHeight:t.globalData.navHeight,
    provinces: [
      ['京', '沪', '粤', '津', '冀', '晋', '蒙', '辽', '吉'],
      ['黑','苏', '浙', '皖', '闽', '赣', '鲁', '豫', '鄂'],
      ['湘','桂', '琼', '渝', '川', '贵', '云', '藏'],
      ['陕', '甘', '青', '宁', '新'],
    ],
    numbers: [
      ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K"],
      ["L", "M", "N", "P", "Q", "R", "S", "T", "U", "V"],
      ["W", "X", "Y", "Z", "港", "澳", "学"]
    ],
  },
  onLoad: function (options) {
    var that=this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          phone_type:res.platform
        })
      }
    })
  },
  getPhoneNumber(e){
    const that = this,
      iv = e.detail.iv,
      da = e.detail.encryptedData,
      carnum = this.data.carnum.toString().replace(/,/g, "")
    console.log(e);
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    wx.checkSession({
      success: (res) => {
        wx.login({
          success(e){
            console.log(e);
            wx.showLoading({
              title: '请稍后',
            })
            t.request('/users/get_session',{code: e.code,iv:iv,da:da},function(event){
              wx.hideLoading({})
              if (event.code!==200) return wx.showToast({ title: event.msg,icon:'none' })
              if (event.code==200) {
                wx.setStorageSync('tel', event.result.phoneNumber)
                that.setData({
                  tel:event.result.phoneNumber,
                })
                that.formSubmit()
              }
            })
          }
        })
      },
    })

    if (this.data.showNewPower&&carnum.length!==8) return wx.showToast({ title: '请输入正确的新能源车牌号',icon:'none'})   
    if (carnum.length!==7) return wx.showToast({ title: '请输入正确的车牌号',icon:'none' })   
  },
  checkboxChange: function(e){
    console.log(e.detail.value);
    if(e.detail.value=='cb'){
      wx.navigateTo({
        url: '/pages/login/notice',
      })
    }
  },
  bindChoose(e) {
    if (!this.data.carnum[6] || this.data.showNewPower) {
      var arr = [];
      arr[0] = e.target.dataset.val;
      this.data.carnum = this.data.carnum.concat(arr)
      this.setData({
        carnum: this.data.carnum,
        choose_num:this.data.carnum.length
      })
    }
  },
  bindDelChoose() {
    if (this.data.carnum.length != 0) {
      this.data.carnum.splice(this.data.carnum.length - 1, 1);
      this.setData({
        carnum: this.data.carnum,
        choose_num:this.data.carnum.length
      })
    }
  },
  showPowerBtn() {
    this.setData({
      showNewPower: !this.data.showNewPower,
      KeyboardState: true,
    })
  },
  closeKeyboard() {
    this.setData({
      KeyboardState: false,
      choose_num:null,
    })
  },
  openKeyboard() {
    this.setData({
      KeyboardState: true,
      choose_num:this.data.carnum.length
    })
  },
  choose_car_type(e){
    this.setData({
      showNewPower:true
    })
  },
  formSubmit(e){
    var carnum = this.data.carnum.toString().replace(/,/g, "")
    if (this.data.showNewPower) {
      if (carnum.length!==8){
        wx.showToast({
          title: '请输入正确的新能源车牌号',
          icon:'none'
        })   
        return;
      }
    }else{
      if (carnum.length!==7){
        wx.showToast({
          title: '请输入正确的车牌号',
          icon:'none'
        })   
        return;
      }
    }
    var o ={
      phone:this.data.tel,
      code:	carnum,
    }
    wx.showLoading({
      title: '请稍等',
    })
    t.request('/cars/add',o,function(e){
      wx.hideLoading({
        success: (res) => {},
      })
      console.log(e);
      if (e.code==200) {
        wx.showToast({
          title: '添加成功',
        })
        t.getCarList()
        t.globalData.show_login = false
        wx.navigateBack({
          delta: 0,
        })
      }else{
        wx.showToast({
          title: e.msg,
          duration:2000,//显示时长
          mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon:'none', //图标，支持"success"、"loading"  
        })
      }
    })
  },
  tip(e){
    wx.showToast({
      title: '暂不用填写',
    })
  },
  navto(e){
    wx.navigateTo({
      url: '/pages/article/article?id=334',
    })
  }
})